package nio;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

/**
 * @Author: Dgg
 * @Date: 2021/11/24 21:49
 */
public class NioChannelTest_1 {

	public static void main(String[] args) {
		String str = "Learn and live.";
		String chinese = "活着，为了学习。";
		FileOutputStream stream = null;
		try {
			// 创建输出流
			stream = new FileOutputStream(FilePathConstants.filePath + str + FilePathConstants.format);

			// 通过输出流获取对应的文件channel
			FileChannel channel = stream.getChannel();
			// 创建缓冲区ByteBuffer
			// 英文符号一个对应一个字节， 汉字utf-8 对应三个字节
			ByteBuffer allocate = ByteBuffer.allocate(1024);

			// 将string 写入到bytebuffer 中
			allocate.put(str.getBytes(StandardCharsets.UTF_8));

			// 对byteBuffer 进行翻转

			allocate.flip();
			channel.write(allocate);
			stream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
